package com.yiguo.gmall_logger.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yiguo.gmall.Constansts;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Log4j //帮你声明了一个logger
public class LogController {
        @Autowired
        private KafkaTemplate<String,String> produce;
        //集群地址


    //Logger logger = Logger.getLogger(LogController.class);
    //value值需要和 mock中 loguploader对应
    @RequestMapping(value = "/log")
    public Object handleLog(String logString){

        JSONObject jsonObject = JSON.parseObject(logString);
        jsonObject.put("ts",System.currentTimeMillis());
        System.out.println(logString);
        log.info(jsonObject);
        //将数据生成到kafka
        //判断jsonObject 生成是类型是 日志数据还是 业务数据
        if ("event".equals(jsonObject.getString("type"))){
            produce.send(Constansts.GMALL_EVENT_LOG,jsonObject.toString());
        }else {
            produce.send(Constansts.GMALL_STARTUP_LOG,jsonObject.toString());
        }

        return  "success";
    }
}
